<?php
// +---------------------------------------------------------------------------+
// | This file is part of the KnowledgebasePublisher package                   |
// | KnowledgebasePublisher - web based knowledgebase publisher tool           |
// |                                                                           |
// | Author:  Evgeny Leontev <eleontev@gmail.com>                              |
// | Copyright (c) 2005 Evgeny Leontev                                         |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

class KBClientPageRenderer
{

	var $site_path;
	var $title_delim = ' / ';

	
    function KBClientPageRenderer(&$view, &$manager) {
		$this->setCommonVars($view, $manager);
		$this->site_path = APP_CLIENT_PATH;
    }

	
	function setCommonVars(&$view, &$manager) {
        $this->view = &$view;
		$this->manager = &$manager;
		$this->vars['base_href'] = $this->view->conf['client_home_path'] . 'client/';	
	}
	
	
	 function render() {
		 
		if($this->isOnePageTemplate()) {
			
			$tpl = &new tplTemplatez($this->getTemplate());
			
			$data['content'] = &$this->view->execute($this->manager);
			$data['content'] .= $this->getCopyright();
			$data['meta_title'] = $this->getMetaTitle();
			$data['meta_charset'] = $this->getMetaCharset();
			$data['meta_robots'] = $this->getMetaRobots($this->view->view_id);
			$data['meta_keywords'] = $this->view->meta_keywords;
			$data['meta_description'] = $this->view->meta_description;
			$data['rss_head_links'] = $this->view->getRssHeadLinks($this->manager);
			//echo "<pre>"; print_r($this->view); echo "</pre>";
			
			$tpl->tplAssign($this->view->css);
			$tpl->tplParse(array_merge($this->vars, $data));
			return $tpl->tplPrint(1);			
		
		} else {

			$page = $this->getHeaderFooter();
			
			include $page[0];
			
			$str = '<link rel="stylesheet" type="text/css" href="%s" />';
			foreach($this->view->css as $v) { 
				$css[] = sprintf($str, $v);
			}
			echo implode("\n", $css) . "\n";
			
			echo $this->view->execute($this->manager);
			echo $this->getCopyright();
			include $page[1];
		}
	}
	
	
	function isOnePageTemplate() {
		return (strpos($this->manager->setting['page_to_load'], ';') === false);
	}
	
	
	function getHeaderFooter() {
		$ar = explode(';', $this->manager->setting['page_to_load']);
		$ar[0] = trim($ar[0]);
		$ar[1] = trim($ar[1]);
		
		return $ar;
	}
	
	
	function getTemplate() {
		if(strtolower($this->manager->setting['page_to_load']) == 'default') {
			$page = $this->view->template_dir . $this->view->page_template; 
		} else {
			$page = $this->manager->setting['page_to_load'];
		}
		
		return $page;
	}
	
	
	// set title of page
	function getMetaTitle() {
		
		$data = array();
		if($this->manager->getSetting('site_title')) {
			$data[] = $this->manager->getSetting('site_title');
			
			if($this->view->view_id == 'files') {
				$data[] = $this->view->msg['file_title_msg'];
			}
		}
		
		if($this->view->meta_title) {
			$data[] = $this->view->meta_title;
		}
		
		return implode($this->title_delim, $data);
	}
	
	
	function getMetaRobots($view) {
		
		// array with view keys for what we use 
		//<meta name="robots" content="all">
		$all_arr = array('index', 'entry', 'glossary', 'rssfeed');
		$meta_robots = 'NONE';
		
		if(in_array($view, $all_arr)) {
			$meta_robots = 'ALL';
		} 
		
		return $meta_robots;
	}
	
	
	function getMetaCharset() {
		return $this->view->conf['lang']['meta_charset'];
	}
	
	
	function getCopyright() {

		$rss_str = '';
		if($this->manager->getSetting('rss_generate') != 'none') {
			$str = '
			<div style="float: right;">
				<a href="%s"><img src="%s/images/icons/rssv2.gif" alt="RSS" width="80" height="15" border="0" /></a>
			</div>';
			$rss_str = sprintf($str, $this->view->_getLink('rssfeed'), $this->site_path . 'client');
		}
        $str = '';
        if ($this->view->controller->view_format_value == 'left') {
            $str .= $this->getLeftSide();
        }
	
		$str .= '</div><div class="blank"></div><div class="footer">%s
				<div align="left" class="copyright">%s 
					<a href="%s" title="%s" target="_blank"><b>%s</b></a> <span>(%s)</span>
				</div></div>';
		
		return sprintf($str, $rss_str,
							 'Powered by',
		                     $this->view->conf['product_www'],
							 'Knowledge base solution',
							 'KBPublisher',
							 'Knowledge base software'
							 );
	}
	
	
	function getCache() {
		
		require_once 'eleontev/Util/ResultCache.php';
		
		$cache = new ResultCache();
		$cache->refresh = true;
		$cache->cache_dir = APP_CACHE_DIR;
		$cache->setUserFuncVars(array($this, 'render'));

		$auth = &new AuthPriv();
		$cache_id = md5('.ht' . $_SERVER['PHP_SELF'] . $_SERVER['QUERY_STRING'] . $auth->getRoleId());
		return $cache->getData($cache_id);
	}
	
	
	function display() {
	
		//timestart("cache");
		//echo $this->getCache();
		//timestop("cache");
		
		//timestart("no cache");
		echo $this->render();
		//timestop("no cache");
	}

    function getLeftSide() {
        return '</div>';
    }
}
?>